<?php

namespace common\helps;

use Yii;
use yii\web\Controller;

/*
 * 自定义全局公共方法
 */
class tools
{


    //ajax 正确返回
    public static function ajax_return($code = 0,$msg = '成功',$data = []){

        $array = ["code" => $code, "msg" => $msg, "data" => $data];

        return json_encode($array,JSON_UNESCAPED_UNICODE);
    }

    //ajx 错误返回
    public static function ajax_return_error($code = 1,$msg = '失败',$data = []){

        $array = ["code" => $code, "msg" => $msg, "data" => $data];

        return json_encode($array,JSON_UNESCAPED_UNICODE);
    }

    //假排序
    public static function fakeOrder($b,$a){
        $order = $b . sprintf("%04d", $a);
        return $order;
    }

    //查找上级
    public static function familyTree($depart,$code)
    {
        $query = new \yii\db\Query();
        $tree = array();
        while (!empty($code)) {
            $depart = $query
                ->select(['ID','departname','org_code','org_type'])
                ->from('ifssc_php.t_s_depart')
                ->where(['org_code'=>$code])
                ->one();
            $tree[] = $depart;
            $code = substr($depart['org_code'],0,-3);
            self::familyTree($depart, $code);


        }
        return $tree;
    }


    //token验证
    public static function checkToken($id,$token)
    {
        $redis = Yii::$app->cache;
        $accessToken = $redis->get($id);
        $tokenTime = substr($accessToken['token'],-10);
        $passTime =  7*24*3600;
        $time = time();
        if(empty($token)){
            return 1; //token不能为空
        }else if($tokenTime + $passTime < $time ){
            return 2;//token过期
        }else if($accessToken['token'] != $token ){
            return 3;//token不正确
        }else{
            return 4;
        }
    }


    //元转万
    public static function tenThousand($price){
        $p = bcdiv($price, 10000, 2);
        return $p;
    }







}